package com.itheima.reggie_auto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie_auto.entity.ShoppingCart;
import com.itheima.reggie_auto.mapper.ShoppingCartMapper;
import com.itheima.reggie_auto.service.ShoppingCartService;
import com.itheima.reggie_auto.util.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    @Override
    public List<ShoppingCart> getAll() {

        //分析  ：条件是根据用户Id查询全部
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(lqw);

        return shoppingCarts;
    }

    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //分析：先判断是添加的套餐还是菜品   然后判断购物车中本来是否有值 ，有值 就修改份数加一，无值便新增一个
        //构造查询条件 ： 购物车与用户ID绑定
        //设置用户Id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        if (shoppingCart.getSetmealId() != null) {
            //新增的套餐
             lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }else{
            //新增的菜品
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }

        //然后判断购物车中本来是否有值
        ShoppingCart cart = shoppingCartMapper.selectOne(lqw);
        if (cart != null) {
            //购物车原本有值
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateById(cart);
        }else{
            //购物车没有此数据
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
            cart = shoppingCart;
        }
        return cart;

    }

    @Override
    public void clean() {
        //分析：根据用户id清空数据
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartMapper.delete(lqw);
    }

    @Override
    public void sub(ShoppingCart shoppingCart) {
        //分析：如果购物车本来存在那么 数量减一  如果为一
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);

        //判断是套餐还是菜品
        if (shoppingCart.getSetmealId() != null) {
            //套餐
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else{
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        //查询 ：
        ShoppingCart cart = shoppingCartMapper.selectOne(lqw);

        if (cart.getNumber()>1) {
            //修改数量
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.updateById(cart);
        }else{
            //删除
            shoppingCartMapper.deleteById(cart.getId());
        }
    }



}
